﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management;
using System.Threading;
using MySqlCodeGeneratorLib;

namespace DBCodeGenerator
{
    public partial class frmmssqllogin : Form
    {
        SqlConnection conn = null;
        string connectionstring = "Server={0};User Id={1}; Password={2};";
        string formatedconnstr;
        UserSettings usettings;
        private bool Status;
      

        public frmmssqllogin()
        {
            InitializeComponent();
            GlobalSettings.Databases = new DataTable();
            CheckForIllegalCrossThreadCalls = false;
            usettings = new UserSettings();
        
        }

        private void BindServers()
        {
            List<string> allserver = usettings.Getallsettings();
            cmbservers.DataSource = allserver;

        }

        private bool Authenticate()
        {
            try
            {
                if (String.IsNullOrEmpty(txtlogin.Text))
                {
                    MessageBox.Show("Invalid user name!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return false;
                }
                else if (String.IsNullOrEmpty(txtpwd.Text))
                {
                    MessageBox.Show("Invalid password!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return false;
                }
                DataTable tbl = new DataTable();
                formatedconnstr  = String.Format(connectionstring, cmbservers.Text.Trim(), txtlogin.Text.Trim(), txtpwd.Text.Trim());
                conn = new SqlConnection(formatedconnstr);
                conn.Open();
                SqlCommand cmd = new SqlCommand("SELECT name FROM sys.databases", conn);
                
                SqlDataAdapter adp = new SqlDataAdapter(cmd);
                adp.Fill(tbl);

                if (tbl.Rows.Count>0)
                {
                    GlobalSettings.Databases = tbl;
                    Status = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                Status = false;
            }
            finally
            {
                if (conn != null && conn.State == ConnectionState.Open)
                    conn.Close();
            }

            if (Status)
            {
                GlobalSettings.Uname = txtlogin.Text.Trim();
                GlobalSettings.Password = txtpwd.Text.Trim();
                GlobalSettings.ServerName = cmbservers.Text.Trim();
                GlobalSettings.ConnectionString = formatedconnstr;
            }

            return Status;
        }

        private void btnlogin_Click(object sender, EventArgs e)
        {

            this.CallAuthProcess();
        }

        private void CallAuthProcess()
        {
            if (Authenticate())
            {
                usettings.SaveSettings(cmbservers.Text);
                this.ShowInTaskbar = false;
                this.Hide();
                frmcsharpsettings set = new frmcsharpsettings();
                set.ShowDialog();
                this.Close();
            }
        }

        private void GetAllAvailableInstances()
        {
            DataTable table = SmoApplication.EnumAvailableSqlServers(false);
            cmbservers.DataSource = table;
            cmbservers.DisplayMember = "name";
            cmbservers.ValueMember = "Server";
        }

        private void frmmssqllogin_Load(object sender, EventArgs e)
        {
           // GetAllAvailableInstances();
            BindServers();
        }

        private void frmmssqllogin_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.CallAuthProcess();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MySqlInformation inf = new MySqlInformation();
           List<string> dblst = inf.Login("localhost", "root", "QtX4300","3306");
        }
    }
}
